Kollision mehrerer Quader erkennen.py
Kollision mehrerer Quader erkennen.py — Python Source, 2 KB (2612 bytes)
Dateiinhalt
from visual import * #Ueberpruefen, ob zwei Quader eine Kollision bilden #Zu Beginn sind beide Quader gruen, bei Kollision werden sie rot #Erzeugen der Quader R1 = box(pos=(-5,0,0), size=(5,2,0),axis=(0,1,0), color=color.green) R2 = box(pos=(5,0,0), size=(2,3,0), axis=(0,1,0), color=color.green) scene.range = 10 # Skalierung wird festgelegt #Bewegung def move(objekt,richtung): objekt.pos += richtung while True: rate(25) #R1 kann durch Cursortasten nach oben, unten, rechts und links bewegt werden if scene.kb.keys: zeichen = scene.kb.getkey() if zeichen == "up": move(R1,(0,1,0)) if zeichen == "down": move(R1,(0,-1,0)) if zeichen == "right": move(R1,(1,0,0)) if zeichen == "left": move(R1,(-1,0,0)) if(R1.axis.y == 0 and R2.axis.y == 0): if((R1.pos.x + R1.length/2 > R2.pos.x - R2.length/2 and R2.pos.x + R2.length/2 > R1.pos.x - R1.length/2 and R1.pos.y + R1.height/2 > R2.pos.y - R2.height/2 and R2.pos.y + R2.height/2 > R1.pos.y - R1.height/2)): R1.color = color.red R2.color = color.red else: R1.color = color.green R2.color = color.green if(R1.axis.x == 0 and R2.axis.y == 0): if((R1.pos.x + R1.height/2 > R2.pos.x - R2.length/2 and R2.pos.x + R2.length/2 > R1.pos.x - R1.height/2 and R1.pos.y + R1.length/2 > R2.pos.y - R2.height/2 and R2.pos.y + R2.height/2 > R1.pos.y - R1.length/2)): R1.color = color.red R2.color = color.red else: R1.color = color.green R2.color = color.green if(R1.axis.y == 0 and R2.axis.x == 0): if((R1.pos.x + R1.length/2 > R2.pos.x - R2.height/2 and R2.pos.x + R2.height/2 > R1.pos.x - R1.length/2 and R1.pos.y + R1.height/2 > R2.pos.y - R2.length/2 and R2.pos.y + R2.length/2 > R1.pos.y - R1.height/2)): R1.color = color.red R2.color = color.red else: R1.color = color.green R2.color = color.green if(R1.axis.x == 0 and R2.axis.x == 0): if((R1.pos.x + R1.height/2 > R2.pos.x - R2.height/2 and R2.pos.x + R2.height/2 > R1.pos.x - R1.height/2 and R1.pos.y + R1.length/2 > R2.pos.y - R2.length/2 and R2.pos.y + R2.length/2 > R1.pos.y - R1.length/2)): R1.color = color.red R2.color = color.red else: R1.color = color.green R2.color = color.green